package controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JTextField;
import javax.swing.JTextPane;

import model.Carnet;
import model.Contact;
/**
 * this class is to set the action listener of the button searbyname in the main window
 * @author Estelle
 *
 */
public class SearchAction implements ActionListener {
	private JTextField first;
	private Carnet carnet = new Carnet(" ");
	private JTextPane person = new JTextPane();
	
	public SearchAction(JTextField FirstName, JTextPane list){
		FileDoc file = new FileDoc();
		carnet.copy(file.recupCarnet("Estelle"));
		first = FirstName;
		person = list;
	
	}
	 @Override
     public void actionPerformed(ActionEvent event) {
		 Contact tmp = new Contact();
		 String f = first.getText().toUpperCase();
		 
		 tmp.copyContact(carnet.searchContact(f));
		 if(tmp.emptyContact()){
			 person.setText("Sorry your contact doesn't exist in your adress book");
		 }
		 else setPersonDetail(tmp);
		 
     }
	 
	 public void setPersonDetail(Contact con){
		 String buff = 
					""+ con.getLastName()+  " " + con.getFirstName() + 
					"\n " + con.getGroupe() +
					"\n\n\t personal numbers :" + con.getPersonalPhone() +
					"\n\n\t HomeAddress : " + con.getHomeAddress() + 
					"\n\n\t CompanyAddress :" + con.getCompanyAddress()+
					"\n\n\t HomePage :" + con.getHomePage() +
					"\n\n\t email :" + con.getEmail() ;
		 person.setText(buff);
	}
}

